
/* GCSx
** TILESET.H
**
** Tileset support, objects and drawing
** Doesn't include any editor-only functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_TILESET_H_
#define __GCSx_TILESET_H_

class TileSet : virtual public LoadOnly {
protected:
    // Is the tile set cached to our file and not in memory?
    int cached;
    FileRead* cacheFile;
    int lockCount;
    int lockCountPlay;

    // Name of tile set, where it's located
    std::string name;
    std::string nameL;
    class World* world; // NULL if not yet added to a world
    
    // Numeric ID- computer-generated- unique to world- nonzero
    int id;
    
    // All tiles are the same size
    int width;
    int height;
    int numTiles; // count doesn't include an always-blank (transparent) tile 0
    
    // If non-zero, the "preferred" number of tiles to display per line when
    // editing; technically editor-only but saved, so always available
    int numTilesPerLine;
    
    // Size information of tile surface- Number of tiles per line
    int tsPerLine;
    
    // Each tile stores a tile with alpha component
    // Format of data is always 32bpp RGBA
    SDL_Surface* tiles; // CACHEABLE
    
    // If new tiles or expanded tiles have a background of black or transparent
    int defaultTransparent;

    // Font tilesets store widths for each character; this pointer is NULL
    // for non-font tilesets
    int isFont;
    Uint32* fontWidths; // CACHEABLE

    // Non-font tilesets store zero or more collision maps; this pointer is NULL
    // if zero collision maps or a font tileset; these maps are bitmasks,
    // stored vertically map-by-map (one Uint32 per 32 pix wide rounded up,
    //  one per pix high, organized columns-first)
    int numCollisionMaps; // count doesn't include always-blank map 0
    int cmWidth; // In Uint32s
    int cmHeight; // In Uint32s
    Uint32* collisionMaps; // CACHEABLE
    
    enum {
        // Max pixels in each direction we can make tile storage surfaces
        // We don't want surfaces over a certain width due to Sint16 calculations
        TILE_SURFACE_MAX = 32767,
        
        // Default per line, when creating a blank set
        TILE_DEFAULT_PERLINE = 16,
        TILE_FONTSET_PERLINE = 16,
        
        // Default counts when properties dialog called with "newSet" true
        TILE_DEFAULT_COUNT = 64,
        COLLISION_DEFAULT_COUNT = 0,
    };
    
    // Texture data
    class TextureMap* texture; // CACHEABLE
    
    // Calculates cmWidth and cmHeight from width and height
    void calculateCmSizes();
    
    // For generating textures
    static const TileSet* activeGenTexture;
    static void genTexture(int position, const SDL_Surface*& src, int& x, int& y);

    int doLock(int play) throw_File;
    int doUnlock(int play);
    void cacheLoadTiles() throw_File;
    
public:
    // All tile numbers are 1-based
    // Id must be unique; use ID of 0 if we're going to load anyways
    // World and ID may be NULL/0 if in process of creating
    TileSet(class World* myWorld, int myId = 0); // Starts with default settings
    virtual ~TileSet();
    
    // Accessors
    int getWidth() const { return width; }
    // Fonts only; Lock required
    // WARNING: asserts tile is within proper range
    int getGlyphWidth(int tile) const;
    int getHeight() const { return height; }
    int getCount() const { return numTiles; }
    int getCollisionCount() const { return numCollisionMaps; }
    int getTilesPerLine() const { return numTilesPerLine; }
    const std::string& getName() const { return name; }
    const std::string& getNameL() const { return nameL; }
    int getIsFont() const { return isFont; }
    int getDefaultTransparent() const { return defaultTransparent; }
    const class World* getWorld() const { return world; }
    class World* getWorld() { return world; }
    int getId() const { return id; }
    int getBlockType() const { return WorldFileLoad::BLOCKTYPE_TILESET; }
    // Returns ptr to surface, stores x/y of tile within that surface
    // Non-play lock required
    // Returns NULL if outside valid range of tiles
    const SDL_Surface* getTileSurface(int tile, int& x, int& y) const;
    
    // File access
    void loadHeader(FileRead* file) throw_File;
    void loadContent(FileRead* file);
    int isContentCached() const;
    void cacheLoad() throw_File;
    // Play lock stores texture; non-play stores surface
    // Double lock if you need both (do regular lock first for efficiency)
    int markLock() throw_File;
    int markLockPlay() throw_File;
    int markUnlock();
    int markUnlockPlay();
    int isLocked() const;
    
    enum {
        // Default tile size, when creating a blank set
        TILE_DEFAULT_SIZE = 32,
        
        // Fixed counts for font sets
        TILE_FONTSET_NORMAL_COUNT = 96,
        TILE_FONTSET_EXT_COUNT = 224,
    };
    
    // Play lock required; NULL if not locked
    const class TextureMap* getTexture();
};

#endif

